<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.69.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>隨機驗證碼 &middot; 郝朝帅的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0f ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/blog/"><h1>郝朝帅的博客</h1></a>
      <p class="lead">
       我的个人博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>隨機驗證碼</h1>
  <time datetime=2020-04-25T20:05:06&#43;0800 class="post-date">Sat, Apr 25, 2020</time>
  <p>#验证码</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#导包</span>
<span style="color:#f92672">from</span> django.http <span style="color:#f92672">import</span> HttpResponse

<span style="color:#75715e">#导入类视图</span>
<span style="color:#f92672">from</span> django.views <span style="color:#f92672">import</span> View

<span style="color:#75715e">#随机库</span>
<span style="color:#f92672">import</span> random

<span style="color:#75715e">#图片库</span>
<span style="color:#f92672">from</span> PIL <span style="color:#f92672">import</span> Image

<span style="color:#75715e">#绘画库</span>
<span style="color:#f92672">from</span> PIL <span style="color:#f92672">import</span> ImageDraw

<span style="color:#75715e">#文件流</span>
<span style="color:#f92672">import</span> io

<span style="color:#75715e">#导入redis数据库</span>
<span style="color:#f92672">import</span> redis
<span style="color:#75715e">#定义地址和端口</span>
host <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;127.0.0.1&#39;</span>
port <span style="color:#f92672">=</span> <span style="color:#ae81ff">6379</span>
<span style="color:#75715e">#建立redis连接</span>
r <span style="color:#f92672">=</span> redis<span style="color:#f92672">.</span>Redis(host<span style="color:#f92672">=</span>host,port<span style="color:#f92672">=</span>port)



<span style="color:#75715e">#定义验证码</span>
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MyCode</span>(View):

    <span style="color:#75715e">#定义随机验证颜色</span>
    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">get_random_color</span>(self):
        R <span style="color:#f92672">=</span> random<span style="color:#f92672">.</span>randrange(<span style="color:#ae81ff">255</span>)
        G <span style="color:#f92672">=</span> random<span style="color:#f92672">.</span>randrange(<span style="color:#ae81ff">255</span>)
        B <span style="color:#f92672">=</span> random<span style="color:#f92672">.</span>randrange(<span style="color:#ae81ff">255</span>)

        <span style="color:#66d9ef">return</span> (R,G,B)

    <span style="color:#75715e">#随机验证码</span>
    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">get</span>(self,request):
        <span style="color:#75715e">#画布</span>
        img_size <span style="color:#f92672">=</span> (<span style="color:#ae81ff">110</span>,<span style="color:#ae81ff">50</span>)
        <span style="color:#75715e">#定义颜色类型</span>
        image <span style="color:#f92672">=</span> Image<span style="color:#f92672">.</span>new(<span style="color:#e6db74">&#34;RGB&#34;</span>,img_size,<span style="color:#e6db74">&#39;white&#39;</span>)
        <span style="color:#75715e">#画笔</span>
        draw <span style="color:#f92672">=</span> ImageDraw<span style="color:#f92672">.</span>Draw(image,<span style="color:#e6db74">&#39;RGB&#39;</span>)
        <span style="color:#75715e">#定义随机字符串</span>
        source <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;0123456789&#39;</span>
        <span style="color:#75715e">#容器，用来接收随机字符串</span>
        code_str <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;&#39;</span>
        <span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">4</span>):
            <span style="color:#75715e">#获取字体颜色</span>
            text_color <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>get_random_color()
            <span style="color:#75715e">#获取字符串</span>
            tmp_num <span style="color:#f92672">=</span> random<span style="color:#f92672">.</span>randrange(len(source))
            <span style="color:#75715e">#获取字符集</span>
            random_str <span style="color:#f92672">=</span> source[tmp_num]
            <span style="color:#75715e">#添加到容器中</span>
            code_str <span style="color:#f92672">+=</span> random_str
            <span style="color:#75715e">#绘制</span>
            draw<span style="color:#f92672">.</span>text((<span style="color:#ae81ff">10</span><span style="color:#f92672">+</span><span style="color:#ae81ff">30</span><span style="color:#f92672">*</span>i,<span style="color:#ae81ff">20</span>),random_str,text_color)
        <span style="color:#75715e">#文件流缓冲区</span>
        buf <span style="color:#f92672">=</span> io<span style="color:#f92672">.</span>BytesIO()
        <span style="color:#75715e">#将图片保存到缓冲区</span>
        image<span style="color:#f92672">.</span>save(buf,<span style="color:#e6db74">&#39;png&#39;</span>)
        <span style="color:#75715e">#将随机码存储到redis中</span>
        r<span style="color:#f92672">.</span>set(<span style="color:#e6db74">&#39;code&#39;</span>,code_str)

        <span style="color:#66d9ef">return</span> HttpResponse(buf<span style="color:#f92672">.</span>getvalue(),<span style="color:#e6db74">&#39;image/png&#39;</span>)
</code></pre></div>
</div>


    </main>

    
  </body>
</html>
